package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.dto.NursingPlanDTO;
import com.zzyl.nursing.domain.vo.NursingPlanVO;
import com.zzyl.nursing.domain.vo.NursingProjectPlanVO;
import com.zzyl.nursing.service.NursingProjectPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVO selectNursingPlanById(Long id) {
        //return nursingPlanMapper.selectById( id);
        NursingPlanVO vo = new NursingPlanVO();
        NursingPlan plan=getById(id);
        if(plan== null){
            throw new RuntimeException("未找到该数据");
        }
        BeanUtils.copyProperties(plan,vo);
        List<NursingProjectPlanVO> volist=nursingProjectPlanService.listPlanById(id);
        vo.setProjectPlans(volist);
        return vo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDTO dto) {
        //保存护理计划
        NursingPlan plan = new NursingPlan();
        BeanUtils.copyProperties(dto,plan);
        this.save(plan);
        //保存关系
    if (dto.getProjectPlans()!=null&& dto.getProjectPlans().size()>0) {
        dto.getProjectPlans().forEach(p -> p.setPlanId(plan.getId()));
        nursingProjectPlanService.saveBatch(dto.getProjectPlans());
    }
        return 1;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlan nursingPlan)
    {
        nursingPlan.setUpdateTime(DateUtils.getNowDate());
        //return nursingPlanMapper.updateNursingPlan(nursingPlan);
        return updateById(nursingPlan) ? 1:0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {

        //return nursingPlanMapper.deleteNursingPlanByIds(ids);
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {

        //return nursingPlanMapper.deleteNursingPlanById(id);
        return removeById(id) ? 1 : 0;
    }
}
